package business.bl;

import java.util.ArrayList;
import java.util.HashMap;


import org.apache.log4j.LogManager;
import org.apache.log4j.Logger;
import org.hibernate.SessionFactory;
import org.hibernate.cfg.AnnotationConfiguration;

import util.ServerResponse;

import business.bo.CandidatoBO;
import business.dto.CandidatoDTO;
import business.dto.SimpleDataDTO;
import business.facade.CandidatoFacade;

public class CandidatoBL implements BasicOperation {
	
	public static Logger logger = LogManager.getLogger(CandidatoBL.class);
	@Override
	public ServerResponse save(HashMap parametersList) throws Exception {
		String servletName = (String) parametersList.get("servletName");
		
		String serverResponseMessage="OK";
        ServerResponse serverResponse = new ServerResponse(servletName, null, serverResponseMessage);
        
        
        try {
  		   CandidatoFacade cf=new CandidatoFacade();
  		   serverResponse=cf.save(parametersList);

  		} catch (Throwable ex) {
  		    // Log the exception.
  		    logger.error("Errore in CandidatoBL", ex);
  		}
        
        
        return serverResponse;
	}
	
	public Integer getSimpleNewCVElements(){
		logger.info("BEGIN - CandidatoBL - getNewCVElements");
		Integer returnValue=new Integer(0);
		
		CandidatoFacade cf=new CandidatoFacade();
		
		returnValue=cf.getNewCVElements();
		
		logger.info("END - CandidatoBL - getNewCVElements");
		return returnValue;
	}
	
	public ServerResponse getNewCVElements(HashMap parametersList){
		logger.info("BEGIN - CandidatoBL - getNewCVElements");
		
		Integer incomingCandidatoCounter=new Integer(0);
		SimpleDataDTO returnValue=new SimpleDataDTO();
		String servletName = (String) parametersList.get("servletName");
	     
		
        String serverResponseMessage="OK";
        ServerResponse serverResponse = new ServerResponse(servletName, null, serverResponseMessage);
		
		CandidatoFacade cf=new CandidatoFacade();
		
		incomingCandidatoCounter=cf.getNewCVElements();
		
		returnValue.setData(incomingCandidatoCounter);
		
		serverResponse.addData(returnValue);
		
		logger.info("END - CandidatoBL - getNewCVElements");
		return serverResponse;
	}
	
	public ServerResponse listIncoming(HashMap parametersList) throws Exception {
		
		String servletName = (String) parametersList.get("servletName");
	     
		
        String serverResponseMessage="OK";
        ServerResponse serverResponse = new ServerResponse(servletName, null, serverResponseMessage);
        
        
		try {
		   CandidatoFacade cf=new CandidatoFacade();
		   serverResponse=cf.listIncoming(parametersList);

		} catch (Throwable ex) {
		    // Log the exception.
		    logger.error("Initial SessionFactory creation failed.", ex);
		}
		
		
//		serverResponse.addData(c1.getDTO());
		return serverResponse;
	}
	@Override
	public ServerResponse list(HashMap parametersList) throws Exception {
		
		String servletName = (String) parametersList.get("servletName");
	     
		
        String serverResponseMessage="OK";
        ServerResponse serverResponse = new ServerResponse(servletName, null, serverResponseMessage);
        
        
		try {
		   CandidatoFacade cf=new CandidatoFacade();
		   serverResponse=cf.list(parametersList);

		} catch (Throwable ex) {
		    // Log the exception.
			logger.error("Errore in CandidatoBL", ex);
		}
		
		
//		serverResponse.addData(c1.getDTO());
		return serverResponse;
	}
	
	@Override
	public ServerResponse delete(HashMap parametersList) throws Exception {
		String servletName = (String) parametersList.get("servletName");
		
		String serverResponseMessage="OK";
        ServerResponse serverResponse = new ServerResponse(servletName, null, serverResponseMessage);
        
        
        try {
  		   CandidatoFacade cf=new CandidatoFacade();
  		   serverResponse=cf.delete(parametersList);

  		} catch (Throwable ex) {
  		    // Log the exception.
  			logger.error("Errore in CandidatoBL", ex);
  		}
        
        
        return serverResponse;
	}

	@Override
	public ServerResponse load(HashMap parametersList) throws Exception {
		
		String servletName = (String) parametersList.get("servletName");
		
		String serverResponseMessage="OK";
        ServerResponse serverResponse = new ServerResponse(servletName, null, serverResponseMessage);
        
        
        try {
  		   CandidatoFacade cf=new CandidatoFacade();
  		   serverResponse=cf.load(parametersList);

  		} catch (Throwable ex) {
  		    // Log the exception.
  			logger.error("Errore in CandidatoBL", ex);
  		}
        
        
        return serverResponse;
	}

	@Override
	public ServerResponse upload(HashMap parametersList) throws Exception {
		// TODO Auto-generated method stub
		return null;
	}
	@Override
	public ServerResponse create(HashMap parametersList) throws Exception {
		
		String servletName = (String) parametersList.get("servletName");
	     

        String serverResponseMessage="OK";
        ServerResponse serverResponse = new ServerResponse(servletName, null, serverResponseMessage);
	
        //test
		
		
//		c1.setName("Salvatore");
//		c1.setEmail("salvatore83@libero.it");
//		c1.setCvPath("./UPLOADED_CV/salvatore.doc");
//		
		
		// SessionFactory from standard hibernate.cfg.xml file
		try {
		   CandidatoFacade cf=new CandidatoFacade();
		   serverResponse=cf.create(parametersList);

		} catch (Throwable ex) {
		    // Log the exception.
			logger.error("Errore in CandidatoBL", ex);
		}
		
		
//		serverResponse.addData(c1.getDTO());
		return serverResponse;
		
		
	}
	public ServerResponse find(HashMap parametersList) throws Exception {
		logger.debug("CandidatoBL.find - BEGIN");
		
		String servletName = (String) parametersList.get("servletName");
	     
		String dateStart1 = (String) parametersList.get("dateStart1");
		
		logger.debug("dateStart1 - "+ dateStart1);
		
        String serverResponseMessage="OK";
        ServerResponse serverResponse = new ServerResponse(servletName, null, serverResponseMessage);
        
        CandidatoFacade cf=new CandidatoFacade();
        serverResponse=cf.find(parametersList);
        
        logger.debug("CandidatoBL.find - END");
        
        return serverResponse;
	}
}
